package com.modelscope.ai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.modelscope.ai.domain.dto.ArticleQueryCriteria;
import com.modelscope.ai.domain.entity.Article;
import com.modelscope.ai.domain.entity.Dike;
import com.modelscope.ai.service.ArticleService;
import com.modelscope.ai.mapper.ArticleMapper;
import com.modelscope.ai.utils.AssertUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Administrator
 * @description 针对表【article】的数据库操作Service实现
 * @createDate 2024-03-15 10:09:32
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Override
    public Page<Article> queryAll(ArticleQueryCriteria criteria) {
        // 用来返回所有用户信息，便于测试
        return page(
                new Page<>(criteria.getPageNum(), criteria.getPageSize()),
                new LambdaQueryWrapper<Article>()
                        .eq(criteria.getType()!=null,Article::getType,criteria.getType())
                        .like(StringUtils.isNotBlank(criteria.getTitle()), Article::getTitle,
                                criteria.getTitle())
        );
    }



    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addArticle(Article article) {
        AssertUtil.isEmpty(article.getId(), "新增id应该为空");
        article.setTotal(0);
        save(article);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateArticle(Article article) {
        AssertUtil.isNotEmpty(article.getId(), "id不应该为空");
        AssertUtil.isNotEmpty(article.getTitle(), "标题为空");
        AssertUtil.isNotEmpty(article.getUrl(), "标题的封面不能为空");
        AssertUtil.isNotEmpty(article.getType(),"合集内容不能为空");
        Article byId = getById(article.getId());
        AssertUtil.isNotEmpty(byId, "id不存在");
        byId.setUrl(article.getUrl());
        byId.setTotal(article.getTotal());
        byId.setTitle(article.getTitle());
        byId.setContent(article.getContent());
        byId.setTags(article.getTags());
        byId.setType(article.getType());
        saveOrUpdate(byId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteArticle(List<Long> ids) {
        removeBatchByIds(ids);
    }
}




